package com.woniu.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.entity.Addr;
import com.woniu.common.utils.ResponseData;
import com.woniu.order.dto.AddrDTO;
import com.woniu.order.dto.LogisticsCompanyDto;
import com.woniu.order.dto.LogisticsInforDTO;
import com.woniu.order.feign.UserFeign;
import com.woniu.order.pojo.LogisticsCompany;
import com.woniu.order.pojo.OrderDetail;
import com.woniu.order.pojo.ZyOrder;
import com.woniu.order.service.LogisticsCompanyService;
import com.woniu.order.mapper.LogisticsCompanyMapper;
import com.woniu.order.service.LogisticsService;
import com.woniu.order.service.OrderDetailService;
import com.woniu.order.service.ZyOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author xue
* @description 针对表【logistics_company】的数据库操作Service实现
* @createDate 2024-08-09 15:41:31
*/
@Service
public class LogisticsCompanyServiceImpl extends ServiceImpl<LogisticsCompanyMapper, LogisticsCompany>
    implements LogisticsCompanyService{
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserFeign userFeign;

    @Autowired
    private LogisticsService logisticsService;

    @Autowired
    private ZyOrderService orderService;

    @Override
    public ResponseData<?> queryShipment(String id) {
        LogisticsCompanyDto logisticsCompanyDto = new LogisticsCompanyDto();
        List<LogisticsCompany> list = list();
        List<OrderDetail> list1 = orderDetailService.lambdaQuery().eq(OrderDetail::getOrderId, id).list();
        Addr addr = userFeign.selectAddr(list1.get(0).getAddressId());
        AddrDTO addrDTO = BeanUtil.copyProperties(addr, AddrDTO.class);
        long l = IdUtil.getSnowflake().nextId();
        logisticsCompanyDto.setLogisticsId(String.valueOf(l));
        logisticsCompanyDto.setLogisticsCompany(list);
        logisticsCompanyDto.setAddrDTO(addrDTO);
        return new ResponseData<>().success(logisticsCompanyDto);
    }

    @Override
    @Transactional
    public Boolean synchronousLogisticsInfo(LogisticsInforDTO logisticsInforDTO) {
        // 物流信息
        logisticsService.save(logisticsInforDTO.getLogistics());
        // 订单状态
        orderService.lambdaUpdate()
                .eq(ZyOrder::getId, logisticsInforDTO.getOrderId())
                .set(ZyOrder::getState, 3)
                .update();
        // 订单详情
        orderDetailService.lambdaUpdate()
                .eq(OrderDetail::getOrderId, logisticsInforDTO.getOrderId())
                .set(OrderDetail::getLogisticsCompanyId, logisticsInforDTO.getCompanyId())
                .set(OrderDetail::getLogisticsName, logisticsInforDTO.getLogistics().getLogisticsCompanyName())
                .set(OrderDetail::getLogisticsId, logisticsInforDTO.getCourierNumber())
                .update();
        return true;
    }
}




